package cn.itcast.nems.journal.service.impl;

import cn.hutool.core.lang.Assert;
import cn.itcast.ic.common.exception.v2.BizExceptionProducer;
import cn.itcast.nems.common.entity.BaseEntity;
import cn.itcast.nems.common.entity.UUIDEntity;
import cn.itcast.nems.common.enumeration.NemsErrorBodyEnum;
import cn.itcast.nems.journal.enumeration.JournalDiscountLineState;
import cn.itcast.nems.journal.service.JournalDiscountLineService;
import cn.itcast.nems.order.dao.entity.JournalDiscountLine;
import cn.itcast.nems.order.dao.mapper.JournalDiscountLineMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 优惠单行相关逻辑
 *
 * @author ChangLu.Gao
 * @version v1.0
 * @program nems-server
 * @description
 * @date 2023/12/08 10:41
 */
@Service
@Slf4j
public class JournalDiscountLineServiceImpl extends ServiceImpl<JournalDiscountLineMapper, JournalDiscountLine>
        implements JournalDiscountLineService {

    @Override
    public List<JournalDiscountLine> queryByJournalDiscountId(@NonNull String journalDiscountId) {
        LambdaQueryWrapper<JournalDiscountLine> condition = new LambdaQueryWrapper<>();
        condition.eq(JournalDiscountLine::getJournalDiscountId, journalDiscountId)
                .eq(BaseEntity::getDeletedFlag, Boolean.FALSE);
        return super.list(condition);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchValidByJournalDiscountId(@NonNull String journalDiscountId) {
        LambdaUpdateWrapper<JournalDiscountLine> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(JournalDiscountLine::getJournalDiscountId, journalDiscountId)
                .eq(BaseEntity::getDeletedFlag, Boolean.FALSE)
                .eq(JournalDiscountLine::getState, JournalDiscountLineState.PENDING.name())
                .set(JournalDiscountLine::getState, JournalDiscountLineState.WAITING.name())
                .set(JournalDiscountLine::getEffectiveDatetime, LocalDateTime.now());
        super.update(updateWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void writeOff(@NonNull String id) {
        LambdaUpdateWrapper<JournalDiscountLine> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(UUIDEntity::getId, id)
                .eq(BaseEntity::getDeletedFlag, Boolean.FALSE)
                .eq(JournalDiscountLine::getState, JournalDiscountLineState.WAITING.name())
                .set(JournalDiscountLine::getState, JournalDiscountLineState.WRITE_OFF.name())
                .set(JournalDiscountLine::getWriteOffDatetime, LocalDateTime.now());
        super.update(updateWrapper);
    }

    @Override
    public List<JournalDiscountLine> queryByStudentIdOrCustomerId(Integer studentId, String customerId) {
        Assert.isFalse(
                StringUtils.isBlank(customerId) &&
                        (null == studentId || studentId < 0),
                BizExceptionProducer.produce(NemsErrorBodyEnum.PARAM_EMPTY, "客户ID或学员ID")
        );
        LambdaQueryWrapper<JournalDiscountLine> condition = new LambdaQueryWrapper<>();
        condition.eq(BaseEntity::getDeletedFlag, Boolean.FALSE);
        condition.eq(JournalDiscountLine::getState, JournalDiscountLineState.WAITING.name());
        if (null != studentId && studentId > 0) {
            condition.eq(JournalDiscountLine::getStudentId, studentId);
        } else {
            if (StringUtils.isNotBlank(customerId)) {
                condition.eq(JournalDiscountLine::getCustomerId, customerId);
            }
        }
        return super.list(condition);
    }
}
